﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CloudClipboard
{
    public partial class MainForm : Form
    {

        private CloudClipBoardBiz oCloudClipBoardBiz = null;

        public static MainForm Current { get; private set; }


        public MainForm()
        {
            InitializeComponent();

            MainForm.Current = this;

            CloudClipboardHelper.Initialize(this.Handle);
            CloudClipboardHelper.AddClipboardFormatListener();

            this.oCloudClipBoardBiz = new CloudClipBoardBiz();

            this.HandleDestroyed += this.MainFrom_HandleDestroyed;

            this.notifyIcon.Icon = Resources.icon_err;

            this.Load += MainFrom_Load;
        }

        void MainFrom_Load(object sender, EventArgs e)
        {
            this.oCloudClipBoardBiz.ConnectToServer();
            CloudClipboardHelper.LoopCall(0, new TimeSpan(0, 0, 0, 0, 500), this.UpdateSendIconVisual);
        }

        private void MainFrom_HandleDestroyed(object sender, EventArgs e)
        {
            CloudClipboardHelper.RemoveClipboardFormatListener();
        }

        protected override void DefWndProc(ref Message m)
        {
            if (m.Msg == CloudClipboardHelper.WM_CLIPBOARDUPDATE)
            {
                oCloudClipBoardBiz.NotifyClipBoardChange();
            }
            
            base.DefWndProc(ref m);
        }

        private bool UpdateSendIconVisual(int time)
        {
            if (oCloudClipBoardBiz.ClientStatus == Types.ClientStatusEnum.Normal)
            {
                this.notifyIcon.Icon = Resources.icon_ok;
            }
            else
            {
                if (time % 2 == 0)
                { 
                    this.notifyIcon.Icon = Resources.icon_normal;
                }
                else
                {
                    switch (oCloudClipBoardBiz.ClientStatus)
                    {
                        case CloudClipboard.Types.ClientStatusEnum.Sending:
                            this.notifyIcon.Icon = Resources.icon_upload;
                            break;
                        case CloudClipboard.Types.ClientStatusEnum.Recving:
                            this.notifyIcon.Icon = Resources.icon_download;
                            break;
                        case CloudClipboard.Types.ClientStatusEnum.Disconnect:
                            this.notifyIcon.Icon = Resources.icon_err;
                            break;
                        default:
                            break;
                    }
                }
            }

            return true;
        }

        private void btnExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.oCloudClipBoardBiz.Close();
            Application.Exit();
        }

    }
}
